﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Model;

public partial class ViewbyProduct_ProductSNsCategory : System.Web.UI.Page
{
    /// <summary>
    /// 此页面只检索测试成功的数据
    /// </summary>
    private Test t;
    private TestItem ti;

    public ViewbyProduct_ProductSNsCategory()
    {
        t = new Test();
        ti = new TestItem();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Button5.Attributes.Add("onclick", "PasteData()");
            //ForCrystalReport(null);
            //ForCrystalReport1(null);
        }
        this.Page.Unload += new EventHandler(Page_Unload);
    }

    void Page_Unload(object sender, EventArgs e)
    {
        this.CrystalReportSource1.Dispose();
        this.CrystalReportSource2.Dispose();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string productSN = this.TextBox2.Text.Trim();
        if (productSN.Length < 10)
        {
            string str1 = "序列号过短，请检查！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
        }
        else if (productSN.Length > 50)
        {
            string str1 = "序列号过长，请检查！";
            Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "click", "<script>alert('" + str1 + "');</script>");
        }
        else if (ProductSNCompare(productSN))
        {
            string str1 = "序列号已在列表中！";
            Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "click", "<script>alert('" + str1 + "');</script>");
        }
        else
        {
            this.ListBox1.Items.Add(productSN);
            this.Label1.Text = this.ListBox1.Items.Count.ToString();
            this.TextBox2.Text = "";
            this.TextBox2.Focus();
        }
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        string str = this.myVal.Value;
        string[] strs = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string st in strs)
        {
            if (!ProductSNCompare(st))
                this.ListBox1.Items.Add(st.Trim());
        }        
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        ListItemCollection lic = new ListItemCollection();
        foreach (ListItem item in this.ListBox1.Items)
        {
            if (item.Selected)
                lic.Add(item);
        }
        foreach (ListItem item in lic)
        {
            this.ListBox1.Items.Remove(item);
        }
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        this.ListBox1.Items.Clear();
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected bool ProductSNCompare(string psn)
    {
        foreach (ListItem item in this.ListBox1.Items)
        {
            if (item.Text == psn)
            {
                return true;
            }
        }
        return false;
    }

    protected void BiuldDT(DataTable dt)
    {
        dt.Columns.Add("ProductSN");
        DataRow dr;
        foreach (ListItem item in this.ListBox1.Items)
        {
            dr = dt.NewRow();
            dr["ProductSN"] = item.Text;
            dt.Rows.Add(dr);
        }
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        if (this.ListBox1.Items.Count == 0)
        {
            string str1 = "请先添加产品序列号！";
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "click", "<script>alert('" + str1 + "');</script>");
        }
        else
        {
            DataTable dt = new DataTable();
            BiuldDT(dt);
            ForCrystalReport(dt);
            ForCrystalReport1(dt);
        }
    }

    protected void ForCrystalReport(DataTable dt)
    {
        CrystalReportSource1.ReportDocument.Load(Server.MapPath("ViewFailTests.rpt"));

        //TextObject txtTestTime = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text1"];
        //txtTestTime.Text = dt1.ToString() + "-" + dt2.ToString();

        IList<TestInfo> list = GetTestData(dt);
        CrystalReportSource1.ReportDocument.SetDataSource(list);
        CrystalReportSource1.DataBind();
        this.CrystalReportViewer1.ReportSource = CrystalReportSource1;
        CrystalReportViewer1.DataBind();
    }

    public IList<TestInfo> GetTestData(DataTable dt)
    {
        IList<TestInfo> tests = new List<TestInfo>();
        tests = t.GetTests_bySNs(dt);
        return tests;
    }

    protected void ForCrystalReport1(DataTable dt)
    {
        CrystalReportSource2.ReportDocument.Load(Server.MapPath("ViewFailTestItems.rpt"));

        //TextObject txtTestTime = (TextObject)this.CrystalReportSource2.ReportDocument.ReportDefinition.ReportObjects["Text12"];
        //txtTestTime.Text = dt1.ToString() + "-" + dt2.ToString();

        IList<TestItemInfo> list = GetTestItemData(dt);
        CrystalReportSource2.ReportDocument.SetDataSource(list);
        CrystalReportSource2.DataBind();
        this.CrystalReportViewer2.ReportSource = CrystalReportSource2;
        CrystalReportViewer2.DataBind();
    }

    public IList<TestItemInfo> GetTestItemData(DataTable dt)
    {
        IList<TestItemInfo> testItems = new List<TestItemInfo>();
        testItems = ti.GetTestItems_bySNs(dt);
        return testItems;
    }

    protected void Button6_Click(object sender, EventArgs e)
    {
        if (this.ListBox1.Items.Count == 0)
        {
            string str1 = "请先添加产品序列号！";
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "click", "<script>alert('" + str1 + "');</script>");
        }
        else
        {
            DataTable dt = new DataTable();
            BiuldDT(dt);
            ForCrystalReport1(dt);
            string ReportFile = Server.MapPath("ViewFailTestItems.rpt");
            string ExcelFileName = Server.MapPath("~/ZedGraphImages/SParamTestData.xls");
            bool msg = ExportToExcel(ReportFile, ExcelFileName);
        }
    }

    public bool ExportToExcel(string ReportFile, string ExcelFileName)
    {
        try
        {
            ReportDocument ReportDOC = new ReportDocument();
            DiskFileDestinationOptions FileOPS = new DiskFileDestinationOptions();
            ExportOptions ExOPS;
            //ReportClass cc = new ReportClass();
            ReportDOC.Load(ReportFile);

            DataTable dt = new DataTable();
            BiuldDT(dt);
            ReportDOC.SetDataSource(GetTestItemData(dt));

            FileOPS.DiskFileName = ExcelFileName;
            ExOPS = ReportDOC.ExportOptions;
            ExOPS.DestinationOptions = FileOPS;
            ExOPS.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
            ExOPS.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;
            ReportDOC.Export();
            Response.Clear();
            Response.Buffer = true;
            EnableViewState = false;
            string filename = "SParamTestData.xls";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8));
            Response.WriteFile(ExcelFileName);
            Response.Flush();
            Response.End();
            Response.Close();
            return true;
        }
        catch
        {
            return false;
        }
    }
}
